﻿using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Graphics;
using System.IO;
using Android.Provider;
using Android.Util;
using Java.IO;

namespace Glasses
{
  

    [Activity(Label = "Glasses", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        ImageView imgGlass;
        ImageView imgPic;
        RelativeLayout abLayout;
        Matrix matrixZoom = new Matrix();

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);   
            abLayout = FindViewById<RelativeLayout >(Resource.Id.RLayout);
            imgGlass = FindViewById<ImageView>(Resource.Id.ImgGlass);
            imgPic = FindViewById<ImageView>(Resource.Id.ImgPic);
            imgGlass.Touch += ViewMove;
            imgPic.Touch += ViewZoom;
		
            Button btnLocalPic = FindViewById<Button>(Resource.Id.BtnLocalPic)
                ,
            btnCamera = FindViewById<Button>(Resource.Id.BtnCamera);
            btnLocalPic.Click += delegate
            {
                Intent intent = new Intent();  
                intent.SetType("image/*");  
                intent.SetAction(Intent.ActionGetContent);  
                StartActivityForResult(intent, 1);
            };

            //string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
            //Android.Net.Uri u=new Android.Net.Uri(
            btnCamera.Click += delegate
            {	
                Intent intent = new Intent(); //调用照相机 
                intent.SetAction(MediaStore.ActionImageCapture); 
                //File file=new File(path,"");
                //Android.Net.Uri url=Android.Net.Uri.FromFile(new Java.IO.File(path,"LastGlasses.jpg"));
                //intent.PutExtra("orientation",0);
                //intent.PutExtra(MediaStore.ExtraOutput, url);

                //广播刷新相册   
//                    Intent intentBc = new Intent(Intent.ActionMediaScannerScanFile);  
//                    intentBc.SetData(imageUri);       
//                    this.SendBroadcast(intentBc); 
                StartActivityForResult(intent, 2);    
            };
          
        }


        float lastX, lastY;

        protected void ViewMove(Object send, View.TouchEventArgs e)
        {
            switch (e.Event.Action)
            {
                case MotionEventActions.Down:
                    lastX = e.Event.RawX;
                    lastY = e.Event.RawY;
                    break;
                case MotionEventActions.Move:
                    float dx = e.Event.RawX - lastX;  
                    float dy = e.Event.RawY - lastY;  
                    float left = imgGlass.Left + dx;  
                    float top = imgGlass.Top + dy;  
                    float right = imgGlass.Right + dx;  
                    float bottom = imgGlass.Bottom + dy;  
                    ((View)send).Layout((int)left, (int)top, (int)right, (int)bottom);
                    lastX = e.Event.RawX;
                    lastY = e.Event.RawY;
                    break;
            }

//            switch (e.Event.Action)
//            {
//                case MotionEventActions.Down:
//                    drag=true;
//
//                    break;
//                case MotionEventActions.Move:
//
//                    break;
//
//                case MotionEventActions.Up:
//                    drag=false;
//                    break;
//            }
        }

        float oldDist = 1;

        protected void ViewZoom(Object send, View.TouchEventArgs e)
        {

            switch (e.Event.Action)
            {
                case MotionEventActions.Move:
                    if (e.Event.PointerCount >= 2)
                    {
                        float newDist = Spacing(e.Event);
                        if ((newDist > oldDist + 1) || (newDist < oldDist - 1))
                        {
                            var v = newDist / oldDist;
                            ZooM(v);
                            oldDist = newDist;
                        }
                    }
                    break;
            }
        }

        public static float Spacing(MotionEvent e)
        {  
            float x = e.GetX(0) - e.GetX(1);  
            float y = e.GetY(0) - e.GetY(1);  
            return FloatMath.Sqrt(x * x + y * y);  
        }

        int picDw;
        int picDh;

        private void ZooM(float f)
        {
            if (f > 1)
            {
                matrixZoom.PostScale(1.01f, 1.01f);
               
                imgPic.ImageMatrix = matrixZoom;
            }
            else if (f < 1)
            {
                matrixZoom.PostScale(0.99f, 0.99f);
                imgPic.ImageMatrix = matrixZoom;
	
            }
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if (data != null)
            {
                Bitmap bitmap = null;
                ImageView imageView = (ImageView)FindViewById(Resource.Id.ImgPic);  
                switch (requestCode)
                {
                    case 1:
                        var uri = data.Data;  
                        ContentResolver cr = this.ContentResolver; 
                        var bm = BitmapFactory.DecodeStream(cr.OpenInputStream(uri));
                        bitmap = bm;
                        break;
                    case 2:
                        var bundle = data.Extras;
                        if (data.Extras != null)
                        {
                            bitmap = (Bitmap)bundle.Get("data");
                        }
                        break;
                }
                if (bitmap != null)
                {
                    imageView.SetImageBitmap(bitmap); 

                    picDw = bitmap.Width;
                    picDh = bitmap.Height;
                }
               
            }
            base.OnActivityResult(requestCode, resultCode, data);
        }


      
    }
   
}